/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.ICharStoChoFixedService;
import com.ce.pms.charge.service.ICharStoChoGeneralService;
import com.ce.pms.charge.service.ICharStoChoMeterService;
import com.ce.pms.charge.service.IChargeItemService;
import com.ce.pms.charge.service.IChargeStaService;
import com.ce.pms.charge.vo.CharStaChoFixedVO;
import com.ce.pms.charge.vo.CharStaChoMeterVO;
import com.ce.pms.charge.vo.ChargeItemVO;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.charge.vo.ChargeStaVO;
import com.ce.pms.common.annotation.NoDACL;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.common.vo.ChooseHouseVO;
import com.ce.pms.house.model.MeterType;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.service.IMeterService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.FlatVO;
import com.ce.pms.house.vo.HouseGrpVO;
import com.ce.pms.house.vo.HouseSearchVO;
import com.ce.util.XMLAbout;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：房间标准选用<br>
 * @author 罗军林 创建日期：2008-8-18
 */
public class CharStaChoAction extends PmsBaseAction{
	ICharStoChoGeneralService charStoChoGeneralService;
	ICharStoChoFixedService charStoChoFixedService;
	ICharStoChoMeterService charStoChoMeterService;

	IChargeItemService chargeItemService;
	IChargeStaService chargeStaService;
	IBuildingService buildingService;
	IHouseService houseService;
	IMeterService meterService;

	/**
	 * 功能描述:初始化主页面<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	@NoDACL
	public void initMain(){
		List<BuildingVO> buildingList=buildingService.getAllBuildingList();
		List<ChargeItemVO> meterChargeItemList=chargeItemService.queryChargeItemListByType(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER, null);
		List<ChargeItemVO> generalChargeItemList=chargeItemService.queryChargeItemListByType(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL, null);
		List<ChargeItemVO> fixChargeItemList=chargeItemService.queryChargeItemListByType(SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED, null);

		setAttribute("meterChargeItemList", meterChargeItemList);
		setAttribute("generalChargeItemList", generalChargeItemList);
		setAttribute("fixChargeItemList", fixChargeItemList);
		setAttribute("buildingList", buildingList);
	}

	/**
	 * 功能描述:收费项目下拉联动方法<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 * @throws Exception
	 */
	public void unitedSelect() throws Exception{
		String selectId = this.getRequest().getParameter("selectId");
		String targetId = this.getRequest().getParameter("targetId");
		String xmlStr=null;
		if("chargeStaID".equals(targetId)){
			List<ChargeStaVO> chargeStaList = chargeStaService.queryAllChargeSatBy(new Long(selectId));
			xmlStr=XMLAbout.convertListToSelectXML(chargeStaList, "staName", "keyword");

		}else if("chargeItemID".equals(targetId)){
			List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
			SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER,selectId);
			xmlStr=XMLAbout.convertListToSelectXML(chargeItemList, "itemName", "keyword");
		}
		renderXML(xmlStr);
	}

	/**
	 * 功能描述:初始化常规费用页面<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initGenneralChoList(){
		setAttribute("totalRows", Integer.valueOf(0));
	}

	/**
	 * 功能描述:初始化走表费用选用列表<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initMeterChoList(){
		setAttribute("totalRows", Integer.valueOf(0));
	}

	/**
	 * 功能描述:初始化固定费用选用列表<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initFixedChoList(){
		setAttribute("totalRows", Integer.valueOf(0));
	}

	public void initChooseHouse(@Read ChooseHouseVO chooseHouseVO){
		List<ChooseHouseVO> buildingCellList=getBuildingCellList(buildingService.getAllBuildingList());
		setAttribute("buildingCellList", buildingCellList);
	}

	public void initHouseList(@Read(key="buildingCellId") String buildingCellId){
		ChooseHouseVO chooseHouseVO=new ChooseHouseVO();
		if(buildingCellId != null){
			chooseHouseVO.setSelectId(buildingCellId);
			List<FlatVO> flatList= houseService.queryHouseListBy(getSearchVO(chooseHouseVO));
			if(flatList!=null && flatList.size()>0){
				List<List> list=getFlatInfoWithFloor(flatList);
				setAttribute("houseList", list);
				setAttribute("maxHouseSize",list.get(getMaxFlatIndex(list)).size());
			}
		}
	}

	public void initAddGenneralCho(){
		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL,null);
		setAttribute("chargeItemList", chargeItemList);
	}


	public void initAddMeterCho(){
//		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
//				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER);
		List<MeterType> meterTypeList = meterService.getMeterTypeList();
		setAttribute("meterTypeList", meterTypeList);
//		setAttribute("chargeItemList", chargeItemList);

	}


	public void initAddFixedCho(){
		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED,null);
		setAttribute("chargeItemList", chargeItemList);
	}

	/**
	 * 功能描述:常规费用选用的查询<br>
	 * @param condition
	 * @param pageSize
	 * @param pageNo<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void queryGenneralChoList(@Read ChargeStaChoVO condition,
									@Read(key="ec_crd",defaultValue="15")int pageSize,
									@Read(key = "ec_p",defaultValue="1")	int pageNo){
		condition.setPageNo(pageNo);
		condition.setPageSize(pageSize);
		condition.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));

		Page page=charStoChoGeneralService.queryCharStoChoGeneral(condition);
		setAttribute("charStaChoList", page.getResult());
		setAttribute("totalRows", Integer.valueOf( Long.valueOf(page.getTotalCount()).intValue() ));
	}

	/**
	 * 功能描述:固定费用选用的查询<br>
	 * @param condition
	 * @param pageSize
	 * @param pageNo<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void queryFixedChoList(@Read ChargeStaChoVO condition,
			@Read(key="ec_crd",defaultValue="15")int pageSize,
			@Read(key = "ec_p",defaultValue="1")	int pageNo){
		condition.setPageNo(pageNo);
		condition.setPageSize(pageSize);
		condition.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		Page page=charStoChoFixedService.queryCharStoChoFixed(condition);
		setAttribute("charStaChoList", page.getResult());
		setAttribute("totalRows", Integer.valueOf( Long.valueOf(page.getTotalCount()).intValue() ));
	}

	/**
	 * 功能描述:走表费用选用的查询<br>
	 * @param condition
	 * @param pageSize
	 * @param pageNo<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void queryMeterChoList(@Read ChargeStaChoVO condition,
			@Read(key="ec_crd",defaultValue="15")int pageSize,
			@Read(key = "ec_p",defaultValue="1")	int pageNo){
		condition.setPageNo(pageNo);
		condition.setPageSize(pageSize);
		condition.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		Page page=charStoChoMeterService.queryCharStoChoMeter(condition);
		setAttribute("charStaChoList", page.getResult());
		setAttribute("totalRows", Integer.valueOf( Long.valueOf(page.getTotalCount()).intValue() ));
	}

	/**
	 * 功能描述:常规费用的房间选用的增加<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void addGenneralCho(@Read ChargeStaChoVO chargeStaChoVO){
		chargeStaChoVO.setCreater(this.getUserId());
		charStoChoGeneralService.addCharStaChoGeneral(chargeStaChoVO);
	}

	/**
	 * 功能描述:初始化常规费用的房间选用页面<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initEditGenneralCho(@Read(key="keyword")Long keyword){
		ChargeStaChoVO chargeStaChoVO=charStoChoGeneralService.findCharStoChoById(keyword);
		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL,null);
		List<ChargeStaVO> chargeStaList=chargeStaService.queryAllChargeSatBy(chargeStaChoVO.getChargeItemID());
		setAttribute("chargeStaChoVO", chargeStaChoVO);
		setAttribute("chargeItemList", chargeItemList);
		setAttribute("chargeStaList", chargeStaList);
	}

	/**
	 * 功能描述:初始化走表费用的房间选用页面<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initEditMeterCho(@Read(key="keyword")Long keyword){
		CharStaChoMeterVO charStaChoMeterVO=charStoChoMeterService.findCharStoChoById(keyword);
		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_METER,charStaChoMeterVO.getMeterTypeCode());
		List<ChargeStaVO> chargeStaList=chargeStaService.queryAllChargeSatBy(charStaChoMeterVO.getChargeItemID());
		setAttribute("charStaChoMeterVO", charStaChoMeterVO);
		setAttribute("chargeItemList", chargeItemList);
		setAttribute("chargeStaList", chargeStaList);
	}

	/**
	 * 功能描述:初始化固定费用的房间选用页面<br>
	 * <br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void initEditFixedCho(@Read(key="keyword")Long keyword){
		CharStaChoFixedVO charStaChoFixedVO=charStoChoFixedService.findCharStoChoById(keyword);
		List<ChargeItemVO> chargeItemList=chargeItemService.queryChargeItemListByType(
				SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_FIXED,null);
		List<ChargeStaVO> chargeStaList=chargeStaService.queryAllChargeSatBy(charStaChoFixedVO.getChargeItemID());
		setAttribute("charStaChoFixedVO", charStaChoFixedVO);
		setAttribute("chargeItemList", chargeItemList);
		setAttribute("chargeStaList", chargeStaList);
	}

	/**
	 * 功能描述:常规费用的房间选用的修改<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void editGenneralCho(@Read ChargeStaChoVO chargeStaChoVO){
		chargeStaChoVO.setCreater(this.getUserId());
		charStoChoGeneralService.editCharStaChoGeneral(chargeStaChoVO);
	}

	/**
	 * 功能描述:固定费用的房间选用的修改<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void editFixedCho(@Read CharStaChoFixedVO charStaChoFixedVO){
		charStaChoFixedVO.setCreater(this.getUserId());
		charStoChoFixedService.editCharStaChoFixed(charStaChoFixedVO);
	}


	/**
	 * 功能描述:走表费用的房间选用的修改<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-21<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void editMeterCho(@Read CharStaChoMeterVO charStaChoMeterVO){
		charStaChoMeterVO.setCreater(this.getUserId());
		charStoChoMeterService.editCharStaChoMeter(charStaChoMeterVO);
	}

	/**
	 * 功能描述:常规费用的数据校验<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-22<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkExistCharStoChoGeneral(@Read ChargeStaChoVO searchCondition,
											@Read(key="oldChargeItemID")Long oldChargeItemID){
		//构建修改前的查询
		ChargeStaChoVO oldCondition=new ChargeStaChoVO();
		oldCondition.setChargeItemID(oldChargeItemID);

		Boolean result=charStoChoGeneralService.checkExistCharStoChoGeneral(searchCondition,oldCondition);
		if(result){
			renderText(getLocaleMessage("charge:staChoTip"));
		}else{
			renderText("false");
		}
	}

	/**
	 * 功能描述:走表费用的数据校验<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-22<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkExistCharStoChoMeter(@Read CharStaChoMeterVO searchCondition,
											@Read(key="oldChargeItemID")Long oldChargeItemID){
		//构建修改前的查询
		CharStaChoMeterVO oldCondition=new CharStaChoMeterVO();
		oldCondition.setChargeItemID(oldChargeItemID);

		Boolean result=charStoChoMeterService.checkExistCharStoChoMeter(searchCondition,oldCondition);
		if(result){
			renderText(getLocaleMessage("charge:meterStaChoTip"));
		}else{
			renderText("false");
		}
	}


	/**
	 * 功能描述:固定费用的数据校验<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-22<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkExistCharStoChoFixed(@Read CharStaChoFixedVO searchCondition,
											@Read(key="oldChargeItemID")Long oldChargeItemID){
		//构建修改前的查询
		CharStaChoFixedVO oldCondition=new CharStaChoFixedVO();
		oldCondition.setChargeItemID(oldChargeItemID);

		Boolean result=charStoChoFixedService.checkExistCharStoChoFixed(searchCondition, oldCondition);
		if(result){
			renderText(getLocaleMessage("charge:staChoTip"));
		}else{
			renderText("false");
		}
	}

	/**
	 * 功能描述:<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void addMeterCho(@Read CharStaChoMeterVO charStaChoMeterVO){
		charStaChoMeterVO.setCreater(this.getUserId());
		charStoChoMeterService.addCharStaChoMeter(charStaChoMeterVO);
	}

	/**
	 * 功能描述:<br>
	 * @param chargeStaChoVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void addFixedCho(@Read CharStaChoFixedVO charStaChoFixedVO){
		charStaChoFixedVO.setCreater(this.getUserId());
		charStoChoFixedService.addCharStaChoFixed(charStaChoFixedVO);
	}

	/**
	 * 功能描述:删除一条房间选用，此方法适用所有房间选用的删除<br>
	 * @param keyword<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void delete(@Read(key="keyword") Long keyword){
		charStoChoGeneralService.deleteCharStaChoGeneral(keyword);
	}



	/** 功能描述:得到所有楼栋和单元的组合信息列表<br>
	 * @param buildingList
	 * @return
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<ChooseHouseVO>  getBuildingCellList(List<BuildingVO> buildingList) {
		List<ChooseHouseVO> buildingCellList = new ArrayList<ChooseHouseVO>();
		for(BuildingVO buildingVO : buildingList){
			Set<HouseGrpVO> houseGrpVos= buildingVO.getHouseGrpVos();
			if(houseGrpVos==null || houseGrpVos.size()==0){
				ChooseHouseVO chooseHouseVO =new ChooseHouseVO();
				chooseHouseVO.setSelectId(buildingVO.getKeyword().toString());
				chooseHouseVO.setSelectText(buildingVO.getName());
				chooseHouseVO.setBuildSeqNo(buildingVO.getSeqNo());
				buildingCellList.add(chooseHouseVO);
				continue;
			}

			for(HouseGrpVO houseGrpVO : houseGrpVos){
				ChooseHouseVO chooseHouseVO =new ChooseHouseVO();
				if(buildingVO.getHasCell().equals("1")){
					chooseHouseVO.setSelectId(buildingVO.getKeyword()+","+houseGrpVO.getKeyword());
					chooseHouseVO.setSelectText(buildingVO.getName()+houseGrpVO.getName()+"单元");
				}else{
					chooseHouseVO.setSelectId(buildingVO.getKeyword()+"");
					chooseHouseVO.setSelectText(buildingVO.getName());
				}
				chooseHouseVO.setBuildSeqNo(buildingVO.getSeqNo());
				buildingCellList.add(chooseHouseVO);
			}
		}
		Collections.sort(buildingCellList,PmsUtilityComparator.getComparator());
		return buildingCellList;
	}


	/**
	 * 功能描述:返回房屋最多的楼层索引<br>
	 * @param houseList
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private int getMaxFlatIndex(List<List> houseList){
		int index=0;
		for(int i=0;i<houseList.size();i++){
			if( houseList.get(index).size() < houseList.get(i).size() ){
				index=i;
			}
		}
		return index;
	}

	/**
	 * 功能描述:将房屋信息list根据楼层组装成新的list<br>
	 * 返回结果：一个楼层一个LIST
	 * @param flatVOList<br>
	 * @return
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<List> getFlatInfoWithFloor(List<FlatVO> flatVOList) {
		List<List> list=new ArrayList<List>();
		for(int i=0;i<flatVOList.size();i++){
			List<FlatVO>  temp=new ArrayList<FlatVO> ();
			for(int j=0;j<flatVOList.size();j++){
				if(flatVOList.get(i).getFloor() == flatVOList.get(j).getFloor()
						&& !temp.contains(flatVOList.get(j))
						&& !isExists(list,flatVOList.get(j))){
					temp.add(flatVOList.get(j));
				}
			}
			if(temp!=null && temp.size()!=0) list.add(temp);
		}
		return list;
	}

	/**
	 * 功能描述:<br>
	 * @param list
	 * @param o
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private boolean isExists(List<List>  list,Object o){
		for(List item : list){
			boolean result=item.contains(o);
			if(result)return result;
		}
		return false;
	}

	private HouseSearchVO getSearchVO(ChooseHouseVO chooseHouseVO){
		if(chooseHouseVO==null){
			return null;
		}
		HouseSearchVO houseSearchVO=new HouseSearchVO();
		houseSearchVO.setBuildingId(chooseHouseVO.getBuildId());
		houseSearchVO.setHouseGrpid(chooseHouseVO.getHouseGrpId());
		return houseSearchVO;
	}



	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	public void setChargeItemService(IChargeItemService chargeItemService) {
		this.chargeItemService = chargeItemService;
	}

	public void setChargeStaService(IChargeStaService chargeStaService) {
		this.chargeStaService = chargeStaService;
	}

	public void setCharStoChoFixedService(
			ICharStoChoFixedService charStoChoFixedService) {
		this.charStoChoFixedService = charStoChoFixedService;
	}

	public void setCharStoChoGeneralService(
			ICharStoChoGeneralService charStoChoGeneralService) {
		this.charStoChoGeneralService = charStoChoGeneralService;
	}

	public void setCharStoChoMeterService(
			ICharStoChoMeterService charStoChoMeterService) {
		this.charStoChoMeterService = charStoChoMeterService;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	/**
	 * @param meterService the meterService to set
	 */
	public void setMeterService(IMeterService meterService) {
		this.meterService = meterService;
	}




}
